package com.budwk.app.util.poiUtils;

import cn.hutool.core.io.IoUtil;
import cn.hutool.http.HttpUtil;
import lombok.Data;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * @author ZY.Wang
 * @since 2023/8/1
 */
@Data
public class UrlImageInfo {

    private float height;
    private float width;

    private float targetHeight;
    private float targetWidth;

    private byte[] bytes;

    public UrlImageInfo(String url, float targetLength) {
        this(url, targetLength, targetLength);
    }

    public UrlImageInfo(String url, float targetWidthUser, float targetHeightUser) {
        byte[] bytes = HttpUtil.downloadBytes(url);
        this.bytes = bytes;

        ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);

        try {
            BufferedImage bufferedImage = ImageIO.read(inputStream);
            this.height = bufferedImage.getHeight();
            this.width = bufferedImage.getWidth();

            if (height > width) {
                // 说明照片是竖直的
                this.targetHeight = targetHeightUser;
                this.targetWidth = new BigDecimal(width + "").divide(new BigDecimal(height + ""), 3, RoundingMode.FLOOR).multiply(new BigDecimal(targetHeightUser + "")).intValue();
            }

            if (width > height) {
                // 说明图片是横向
                BigDecimal size = new BigDecimal(height + "").divide(new BigDecimal(width + ""), 3, RoundingMode.CEILING);
                this.targetHeight = size.multiply(new BigDecimal(targetWidthUser + "")).setScale(3, RoundingMode.CEILING).floatValue() + 0.1F;
                this.targetWidth = targetWidthUser;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            IoUtil.close(inputStream);
        }
    }


    /**
     * 目前用于excel获取图片大小
     *
     * @param bytes       字节
     * @param fixedHeight 固定高度
     * @param fixedWidth  固定宽度
     */
    public UrlImageInfo(byte[] bytes, int fixedHeight, Integer fixedWidth) {
        this.bytes = bytes;
        ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);

        try {
            BufferedImage bufferedImage = ImageIO.read(inputStream);
            this.height = bufferedImage.getHeight();
            this.width = bufferedImage.getWidth();

            targetHeight = fixedHeight;
            targetWidth = fixedWidth;

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            IoUtil.close(inputStream);
        }
    }

    /**
     * 目前用于excel获取图片大小（按照横图、竖图，固定高度和固定宽度）
     *
     * @param bytes 字节
     */
    public UrlImageInfo(byte[] bytes) {
        this.bytes = bytes;
        ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);

        try {
            BufferedImage bufferedImage = ImageIO.read(inputStream);
            this.height = bufferedImage.getHeight();
            this.width = bufferedImage.getWidth();

//            if (height > width) {
//                targetHeight = 150;
//                targetWidth = 122;
//            } else {
//                targetHeight = 150;
//                targetWidth = 198;
//            }

            targetHeight = 150;
            targetWidth = 198;

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            IoUtil.close(inputStream);
        }
    }
}
